/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface.AddRegions;

import Business.Launcher.Admin.LaunchAdmin;
import Business.RegionOperation.RegionOperation;
import Business.ShowRegion.ShowRegion;
import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author hooting
 */
public class AddRegionsJFrame extends javax.swing.JFrame {

	/**
	 * Creates new form AddRegionsJFrame
	 */
	public AddRegionsJFrame() {
		initComponents();
		this.setLocationRelativeTo(null);
		showCountry();
		showProvincy();
		showHospital();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmbCountry = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cmbProvincy = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cmbHospital = new javax.swing.JComboBox();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon("resource/images.jpg")); // NOI18N

        jLabel2.setText("Country");

        cmbCountry.setEditable(true);
        cmbCountry.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCountryItemStateChanged(evt);
            }
        });

        jLabel3.setText("Provincy/State");

        cmbProvincy.setEditable(true);
        cmbProvincy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbProvincyItemStateChanged(evt);
            }
        });

        jLabel4.setText("Hospital");

        cmbHospital.setEditable(true);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbCountry, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbProvincy, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbHospital, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnDelete)
                .addGap(65, 65, 65))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbProvincy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbHospital, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnDelete))
                .addGap(0, 31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
		String country = (String)cmbCountry.getSelectedItem();
		String provincy = (String)cmbProvincy.getSelectedItem();
		String hospital = (String)cmbHospital.getSelectedItem();
		if(country == null || country.length() == 0){
			country = "others";
			provincy = "others";
			hospital = "others";
		}else if(provincy == null || provincy.length() == 0){
			provincy = "others";
			hospital = "others";
		}else if(hospital == null || hospital.length() == 0){
			hospital = "others";
		}
		RegionOperation ro = new RegionOperation();
		if(ro.updateRegion(country,provincy,hospital)){
			JOptionPane.showMessageDialog(null, "Update region successfully",
				"Information", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(null, "Updated region already existed",
				"Warning", JOptionPane.WARNING_MESSAGE);
		}
		showCountry();
		showProvincy();
		showHospital();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
		String country = (String)cmbCountry.getSelectedItem();
		String provincy = (String)cmbProvincy.getSelectedItem();
		String hospital = (String)cmbHospital.getSelectedItem();
		if(country == null || country.length() == 0){
			country = "others";
			provincy = "others";
			hospital = "others";
		}else if(provincy == null || provincy.length() == 0){
			provincy = "others";
			hospital = "others";
		}else if(hospital == null || hospital.length() == 0){
			hospital = "others";
		}
		RegionOperation ro = new RegionOperation();
		if(ro.deleteRegion(country,provincy,hospital)){
			JOptionPane.showMessageDialog(null, "Delete region successfully",
				"Information", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(null, "Delete region not existed",
				"Warning", JOptionPane.WARNING_MESSAGE);
		}
		showCountry();
		showProvincy();
		showHospital();

    }//GEN-LAST:event_btnDeleteActionPerformed

    private void cmbCountryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCountryItemStateChanged
        // TODO add your handling code here:
		showProvincy();
		showHospital();
    }//GEN-LAST:event_cmbCountryItemStateChanged

    private void cmbProvincyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbProvincyItemStateChanged
        // TODO add your handling code here:
		showHospital();
    }//GEN-LAST:event_cmbProvincyItemStateChanged

	protected void processWindowEvent(WindowEvent e){
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
			dispose();
			LaunchAdmin.showAdminJFrame();
		}else{
			super.processWindowEvent(e);
		}
	}
	private void showCountry(){
		String[] countries = new ShowRegion().getCountries();
		if(countries == null){
			cmbCountry.setModel(new DefaultComboBoxModel());
			cmbProvincy.setModel(new DefaultComboBoxModel());
			cmbHospital.setModel(new DefaultComboBoxModel());
			return;
		}
		cmbCountry.setModel(new DefaultComboBoxModel(countries));
	}

	private void showProvincy(){
		String country = (String)cmbCountry.getSelectedItem();
		if(country == null || country.length() == 0){
			cmbProvincy.setModel(new DefaultComboBoxModel());
			cmbHospital.setModel(new DefaultComboBoxModel());
			System.out.println("country is null");
			return;
		}
		String[] provincies = new ShowRegion().getProvincies(country);
		if(provincies == null){
			return;
		}
		cmbProvincy.setModel(new DefaultComboBoxModel(provincies));
	}

	private void showHospital(){
		String country = (String)cmbCountry.getSelectedItem();
		if(country == null || country.length() == 0){
			cmbHospital.setModel(new DefaultComboBoxModel());
			return;
		}
		String provincy = (String)cmbProvincy.getSelectedItem();
		if(provincy == null || provincy.length() == 0){
			return;
		}
		String[] hospitals = new ShowRegion().getHospitals(country, provincy);
		if(hospitals == null){
			return;
		}
		cmbHospital.setModel(new DefaultComboBoxModel(hospitals));
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JComboBox cmbCountry;
    private javax.swing.JComboBox cmbHospital;
    private javax.swing.JComboBox cmbProvincy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
